
//加载页面上时 执行里面的方法，。
$(function () {
    selectuser();
})

//通过id编辑===============================================
function edit(id) {
    localStorage.setItem("userId",id);
    $("#right").load("/back/html/user/updateUser.html");
}

//通过id删除==============================================
function del(id) {
    let data = myAjax('/back/user/deleteById',{id:id},'get');
    if (data.result==1){
        alert("删除成功");
        $('#right').load('/back/html/user/usermanager.html');
    }else if(data.result==0){
        alert("删除失败");
        $('#right').load('/back/html/user/usermanager.html');
    }
}
//模糊查询一些数据
function selectuser() {
    let data1={
        username:$("#username").val(),
        phone:$("#phone").val(),
        sex:$("#sex").val(),
        firstbirthday:$("#firstbirthday").val(),
        lastbirthday:$("#lastbirthday").val(),
        hobby:hb(),
        pageSize:$("#pageSize").val(),
        pageNumber:$("#pageNumber").val(),
        sortId:$("#sortId").val(),
        sortUserName:$("#sortUserName").val(),
        sortSex:$("#sortSex").val(),
        sortBirthday:$("#sortBirthday").val(),
        sortCreatetime:$("#sortCreatetime").val(),
        sortUpdatetime:$("#sortUpdatetime").val(),
    }
    let data = myAjax('/back/user/findUser',data1,'get');
    let list = data.list;
    let html='';
    $("#count").html(data.count);
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let a = Math.ceil(count/pageSize);
    $("#pageCount").html(a);
    //页数 改变 上一页下一页颜色=================================================================
    let pageNumber= $("#pageNumber").val();
    if (pageNumber<=1){
        $("#lastPage").attr('disabled',"true");
        $("#lastPage").css("background-color","silver");
        $("#nextPage").removeAttr("disabled");
        $("#nextPage").removeAttr('style',"");

    }else if (pageNumber==a){
        $("#nextPage").attr('disabled',"true");
        $("#lastPage").removeAttr("disabled");
        $("#nextPage").css("background-color","silver");
        $("#lastPage").removeAttr('style',"");
    }
    for (let i = 0;i<list.length;i++){
        html+='<tr>\n' +
            '            <td>'+list[i].id+'</td>\n' +
            '            <td>'+list[i].username+'</td>\n' +
            '            <td>'+list[i].phone+'</td>\n' +
            '            <td>'+list[i].sex+'</td>\n' +
            '            <td>'+list[i].birthday+'</td>\n' +
            '            <td>'+list[i].hobby+'</td>\n' +
            '            <td>'+list[i].introduce+'</td>\n' +
            '            <td>'+list[i].createtime+'</td>\n' +
            '            <td>'+list[i].updatetime+'</td>\n' +
            '            <td><a href="javascript:edit('+list[i].id+')">编辑</a>&nbsp;' +
            '            <a href="javascript:del('+list[i].id+')">删除</a>' +
            '        </td>\n' +
            '        </tr>';
    }
    $("#findUser").html(html);

}
function hb() {
    let hb=[];
    $("input:checkbox[name='hobby']:checked").each(function () {
        let state = ($(this).prop('checked'));
        if (state){
            hb.push($(this).val());
        }
    });
    hb = hb.join(',');
    return hb;

}

//分页查找=================================================
function firstPage() {
    $("#pageNumber").val(1);
    $("#nextPage").removeAttr('style',"");
    selectuser();
}
function lastPage() {
    let a = parseInt($("#pageNumber").val());
    if (a>1){
        $("#pageNumber").val(($("#pageNumber").val()-1));
        $("#nextPage").removeAttr('style',"");
    }
    selectuser();
}
function nextPage() {
    let a = parseInt($("#pageNumber").val());
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let b = Math.ceil(count/pageSize);
    if (a<b){
        $("#lastPage").removeAttr('style',"");
        $("#pageNumber").val(a+1);
    }
    selectuser();
}
function changes() {
    let a = parseInt($("#pageNumber").val());
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let b = Math.ceil(count/pageSize);
    if (a<=1){
        $("#pageNumber").val(1);
        $("#lastPage").attr('disabled',"true");
    }else if (a>=b){
        $("#pageNumber").val(b);
        $("#nextPage").attr('disabled',"true");
    }else {
        $("#lastPage").removeAttr("disabled");
    }
}
function endPage() {
    let count = $("#count").text();
    let pageSize = $("#pageSize").val();
    let a = Math.ceil(count/pageSize);
    $("#pageNumber").val(a);
    $("#lastPage").removeAttr('style',"");
    selectuser();
}
//排序======================================================
function sortId() {
    $("#sortUserName").val("");
    $("#sortSex").val("");
    $("#sortBirthday").val("");
    $("#sortCreatetime").val("");
    $("#sortUpdatetime").val("");
    let a = $("#sortId").val();
    let b= "desc";
    $("#sortId").val((a.match(b)? "asc":"desc"));
    selectuser();

}
function sortUserName() {
    let a = $("#sortUserName").val();
    let b= "asc";
    $("#sortId").val("");
    $("#sortSex").val("");
    $("#sortBirthday").val("");
    $("#sortCreatetime").val("");
    $("#sortUpdatetime").val("");
    $("#sortUserName").val((a.match(b)? "desc":"asc"));
    selectuser();
}
function sortSex() {
    let a = $("#sortSex").val();
    let b= "asc";
    $("#sortId").val("");
    $("#sortUserName").val("");
    $("#sortBirthday").val("");
    $("#sortCreatetime").val("");
    $("#sortUpdatetime").val("");
    $("#sortSex").val((a.match(b)? "desc":"asc"));
    selectuser();
}
function sortBirthday() {
    let a = $("#sortBirthday").val();
    let b= "asc";
    $("#sortId").val("");
    $("#sortUserName").val("");
    $("#sortSex").val("");
    $("#sortCreatetime").val("");
    $("#sortUpdatetime").val("");
    $("#sortBirthday").val((a.match(b)? "desc":"asc"));
    selectuser();
}
function sortCreatetime() {
    let a = $("#sortCreatetime").val();
    let b= "asc";
    $("#sortId").val("");
    $("#sortUserName").val("");
    $("#sortSex").val("");
    $("#sortBirthday").val("");
    $("#sortUpdatetime").val("");
    $("#sortCreatetime").val((a.match(b)? "desc":"asc"));
    selectuser();
}
function sortUpdatetime() {
    let a = $("#sortUpdatetime").val();
    let b= "asc";
    $("#sortId").val("");
    $("#sortUserName").val("");
    $("#sortSex").val("");
    $("#sortBirthday").val("");
    $("#sortCreatetime").val("");
    $("#sortUpdatetime").val((a.match(b)? "desc":"asc"));
    selectuser();
}
//按下enter键就可以查询
function selectuserin() {
    if (event.keyCode== 13){
        $("#slelectUser").click();
    }
}
//清空数据===================================================